package model.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import model.bean.Evento;
import model.bean.Tipo;
import model.interfaces.ITipo;

public class TipoDAO implements ITipo {
	
	public EntityManager getEntitymanager(){
		EntityManagerFactory factory = null;
		EntityManager manager = null;
		
		try{
			factory = Persistence.createEntityManagerFactory("SmartClassroomPUnit");
			manager = factory.createEntityManager();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			factory.close();
		}
		
		return manager;
	}

	@Override
	public void cadastraTipo(Tipo tipo) {
		EntityManager manager = getEntitymanager();
		try{
			manager.getTransaction().begin();
			System.out.println("Cadastrando Tipo " + tipo.getDescricao());
			manager.persist(tipo);
			manager.getTransaction().commit();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			manager.close();
			System.out.println("Participante Inserido com sucesso...");
		}
	}

	@Override
	public void vincularEventoTipo(Evento evento, Tipo tipo) {
		EntityManager manager = getEntitymanager();
		try{
			manager.getTransaction().begin();
			System.out.println("Vinculando Evento ao seu Tipo" + tipo.getDescricao());
			tipo.getEventos().add(evento);
			manager.persist(tipo);
			manager.getTransaction().commit();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			manager.close();
			System.out.println("Participante Inserido com sucesso...");
		}		
	}
	
	
	
}
